<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        .img {
            width: 500px;
            height: 500px;
            background-color: aqua;
        }
    </style>
</head>

<body>
    <div class="img"></div>
    <button>随机</button>
    <script>
        var imgArr = [
            "https://img1.baidu.com/it/u=2285603811,1509329120&fm=253&fmt=auto&app=138&f=JPEG?w=333&h=500",
            "https://img0.baidu.com/it/u=2319909669,883224056&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800",
            "https://img0.baidu.com/it/u=4130957219,3244592604&fm=253&fmt=auto&app=120&f=JPEG?w=440&h=656",
            "https://img2.baidu.com/it/u=4089057245,1794611068&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800",
            "https://img1.baidu.com/it/u=3360032985,25549252&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
            "https://img0.baidu.com/it/u=857510153,4267238650&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675",
        ];
        var btn = document.querySelector("button");
        var tp = document.querySelector(".img");

        var zd = -1;
        btn.onclick = function() {
            var k = randomNum(0, imgArr.length - 1);
            while (zd == k) {
                k = randomNum(0, imgArr.length - 1);
            }
            zd = k;
            console.log(k);
            tp.style.backgroundImage = `url(${imgArr[k]})`;
        };

        function randomNum(min, max) {
            return parseInt(Math.random() * (max + 1 - min)) + min;
        }
    </script>
</body>

</html>